//
//  movieViewController.m
//  movie
//
//  Created by 李丹阳 on 15/7/17.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "movieViewController.h"
#import "movieModal.h"
#import "movieTableViewCell.h"
#import "dataService.h"


#define Kwidth  self.view.bounds.size.width
#define Kheight self.view.bounds.size.height

@interface movieViewController (){
    BOOL isLeft;
    NSMutableArray* modalArray;
    
}

@end

@implementation movieViewController

#pragma mark - baseLoad
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"北美榜";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNavigation];
    [self _createTableView];
    [self _createPosterView];
    [self loadData];
//    [self.ta]
//    modalArray = [[NSMutableArray alloc]init];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - createq
- (void)viewDidAppear:(BOOL)animated{
    
    NSString *str = NSStringFromUIEdgeInsets([_movieListTableView contentInset]);
    NSLog(@"%@",str);
    
}

- (void)_createNavigation{
    UIView *fipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [button1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    button1.tag = 1;
    
    [button1 sizeToFit];
    [button1 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [fipView addSubview:button1];
    
    
    UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [button2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    button2.tag = 2;
    
    [button2 sizeToFit];
    [button2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [fipView addSubview:button2];
    button2.hidden = YES;
    
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc]initWithCustomView:fipView
                                  ];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)_createTableView{
    
    _movieListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, Kheight)];
    
    _movieListTableView.dataSource = self;
    _movieListTableView.delegate = self;
    
    _movieListTableView.hidden = YES;
    

    [self.view addSubview:_movieListTableView];
    
    //注册
    UINib* nib = [UINib nibWithNibName:@"movieTableViewCell" bundle:[NSBundle mainBundle]];
//    [_movieListTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    [_movieListTableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (void)_createPosterView{
    _posterView = [[UIView alloc]initWithFrame:self.view.bounds];
    _posterView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_posterView];
}

- (void)loadData{
    modalArray = [[NSMutableArray alloc]init];

//    NSString* filePath = [[NSBundle mainBundle]pathForResource:@"us_box" ofType:@"json"];
//    NSData* data = [NSData dataWithContentsOfFile:filePath];
//    
//    NSDictionary* dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSDictionary* dataDic = [dataService getJSONDataFromFile:@"us_box"];
    NSArray* subjects = [dataDic objectForKey:@"subjects"];
    
    
    for (NSDictionary* dic in subjects) {
        NSDictionary* subject = [dic objectForKey:@"subject"];
        movieModal* modal = [[movieModal alloc]init];
        modal.title = [subject objectForKey:@"title"];
        modal.year = [subject objectForKey:@"year"];
        NSNumber* number = [[subject objectForKey:@"rating"] objectForKey:@"average"];
        modal.average = [number floatValue];
        modal.image = [subject objectForKey:@"images"];
//        NSLog(@"%@",modal);
        [modalArray addObject:modal];
    }
//    NSLog(@"%@",modalArray);
}

#pragma mark - Action

- (void)flipAction:(UIButton *)btn{
    UIView* flipView = self.navigationItem.rightBarButtonItem.customView;
    
    UIButton* btn1 = (UIButton*)[flipView viewWithTag:1];
    UIButton* btn2 = (UIButton*)[flipView viewWithTag:2];
    
    
    isLeft = !isLeft;
    [self _flipView:flipView isLeft:isLeft];
    [self _flipView:self.view isLeft:isLeft];
    
    
    _movieListTableView.hidden = !_movieListTableView.hidden;
    _posterView.hidden = !_posterView.hidden;
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
}

- (void)_flipView:(UIView* )view isLeft:(BOOL)isleft{
    UIViewAnimationOptions option = isleft?UIViewAnimationOptionTransitionFlipFromLeft :UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:view duration:.5 options:option animations:nil completion:nil];

}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"%li",modalArray.count);
    return (modalArray.count);
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    movieTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    movieModal* movieModal = modalArray [indexPath.row];
    UIImageView * imgeView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tab_bg_all"]];
    imgeView.frame = cell.contentView.frame;
    cell.backgroundView = imgeView;
    cell.modal = movieModal;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
